// 权限控制
import store from '@/store'
import router from '@/router'
const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  // 获取token
  const token = store.state.user.token
  if (token) {
    // 登陆后无法跳转登录页
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.userName) {
        store.dispatch('user/fetchUserInfo')
      }
      // 放行
      next()
    }
  } else {
    // 白名单放行
    if (whiteList.includes(to.path)) {
      next()
      // 跳转登录页
    } else {
      next('/login')
    }
  }
})
